package com.mall.mallDemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.mallDemo.exception.ExceptionEnum;
import com.mall.mallDemo.exception.MyException;
import com.mall.mallDemo.mapper.ProductMapper;
import com.mall.mallDemo.pojo.Product;
import com.mall.mallDemo.service.ProductService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lii
 * @create 2022-06-15 10:32
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    public List<Product> getProductByCategoryId(Integer categoryId) {
        List<Product> list = null;
//        Example example = new Example(Product.class);
//        example.orderBy("productSales").desc();
//        example.createCriteria().andEqualTo("categoryId", categoryId);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("product_sales").eq("category_id", categoryId);

        PageHelper.startPage(0, 8);
        try {
            list = productMapper.selectList(queryWrapper);
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new MyException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return list;
    }

    public List<Product> getHotProduct() {

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("product_sales");

        PageHelper.startPage(0, 8);
        List<Product> list = null;
        try {
            list = productMapper.selectList(queryWrapper);
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new MyException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return list;
    }

    public Product getProductById(String productId) {
        Product product = null;
        try {
            product = productMapper.selectById(productId);
            if (product == null) {
                throw new MyException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return product;
    }

    @Override
    public List<Product> getProductBySearch(String productName) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_name",productName);

        PageHelper.startPage(0, 8);
        List<Product> list = null;
        try {
            list = productMapper.selectList(queryWrapper);
            if (ArrayUtils.isEmpty(list.toArray())) {
                throw new MyException(ExceptionEnum.GET_PRODUCT_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.GET_PRODUCT_ERROR);
        }
        return list;
    }

    public PageInfo<Product> getProductByPage(String currentPage, String pageSize, String categoryId) {
        List<Product> list = null;
        PageHelper.startPage(Integer.parseInt(currentPage) - 1, Integer.parseInt(pageSize), true);
        if (categoryId.equals("0")) { // 为0，代表分页查询所有商品
            list = productMapper.selectList(null);
        }else {
            // 分类分页查询商品
            Product product = new Product();
            product.setCategoryId(Integer.parseInt(categoryId));
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id",Integer.parseInt(categoryId));

            LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                                                                                                                                      lambdaQueryWrapper.like(Strings.isNotEmpty(categoryId), Product::getCategoryId, categoryId);
            list = productMapper.selectList(lambdaQueryWrapper);
        }
        PageInfo<Product> pageInfo = new PageInfo<Product>(list);
        return pageInfo;
    }

    public PageInfo<Product> getProductByPageSearch(String currentPage, String pageSize, String productName) {
        List<Product> list = null;
        PageHelper.startPage(Integer.parseInt(currentPage) - 1, Integer.parseInt(pageSize), true);
        if (productName.equals("")) { //代表分页查询所有商品
            list = productMapper.selectList(null);
        }else {
            // 分类分页查询商品
            Product product = new Product();
            product.setProductName((productName));
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_Name",productName);

            LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.like(Strings.isNotEmpty(productName), Product::getProductName,productName);
            list = productMapper.selectList(lambdaQueryWrapper);
        }
        PageInfo<Product> pageInfo = new PageInfo<Product>(list);
        return pageInfo;
    }
    public PageInfo<Product> getProductByPageSearch(String currentPage, String pageSize, String productName,Integer beginPrice,Integer endPrice) {
        List<Product> list = null;
        PageHelper.startPage(Integer.parseInt(currentPage) - 1, Integer.parseInt(pageSize), true);
        if (productName.equals("")||(endPrice.equals("")&&beginPrice.equals(""))) { //代表分页查询所有商品
            list = productMapper.selectList(null);
        }else {

            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_Name",productName).between("product_selling_price",beginPrice,endPrice);
            list = productMapper.selectList(queryWrapper);
        }
        PageInfo<Product> pageInfo = new PageInfo<Product>(list);
        return pageInfo;
    }
}
